package org.nimitz.test.resteasy;

import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;
import org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap;
import org.jboss.resteasy.plugins.spring.SpringContextLoaderListener;
import org.jboss.resteasy.plugins.spring.i18n.Messages;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContextEvent;

/**
 * Created by nimtz on 2017/5/6.
 */
@Configuration
public class ResteasyAutoConfiguration2 {
//    @Bean
//    ResteasyBootstrap bootstrap() {
//        return new ResteasyBootstrap();
//    }
//
    @Bean
    SpringContextLoaderListener springContextLoaderListener(WebApplicationContext context) {
        return new SpringContextLoaderListener(context) {
            @Override
            public void contextInitialized(ServletContextEvent event)
            {
                boolean scanProviders = false;
                boolean scanResources = false;

                String sProviders = event.getServletContext().getInitParameter("resteasy.scan.providers");
                if (sProviders != null)
                {
                    scanProviders = Boolean.valueOf(sProviders.trim());
                }
                String scanAll = event.getServletContext().getInitParameter("resteasy.scan");
                if (scanAll != null)
                {
                    boolean tmp = Boolean.valueOf(scanAll.trim());
                    scanProviders = tmp || scanProviders;
                    scanResources = tmp;
                }
                String sResources = event.getServletContext().getInitParameter("resteasy.scan.resources");
                if (sResources != null)
                {
                    scanResources = Boolean.valueOf(sResources.trim());
                }

                if (scanProviders || scanResources)
                {
                    throw new RuntimeException(Messages.MESSAGES.cannotUseScanParameters());
                }


            }
        };
    }

//    @Bean
//    HttpServletDispatcher httpServletDispatcher() {
//        HttpServletDispatcher s = new HttpServletDispatcher();
//        return s;
//    }
}
